package top.sehnsucht.dao.impl;

import com.sun.org.apache.bcel.internal.generic.NEW;
import top.sehnsucht.dao.ArticleDao;
import top.sehnsucht.entity.Article;
import top.sehnsucht.entity.Student;
import top.sehnsucht.util.DBUtil;

import java.io.FileInputStream;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * @Description:
 * @Author: Cai
 * @CreateTime: 2021/12/21
 */
public class ArticleDaoImpl implements ArticleDao {
    @Override
    public ArrayList<Article> selectAllArticle() {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        ArrayList<Article> articles = new ArrayList<>();
        try {
            conn = DBUtil.getConn();
            String sql = "select * from article";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                Integer id = rs.getInt(1);
                Integer studentId = rs.getInt(2);
                String title = rs.getString(3);
                String content = rs.getString(4);
                java.util.Date date = rs.getDate(5);
                articles.add(new Article(id, studentId, title, content, date));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return articles;
    }

    @Override
    public ArrayList<Article> selectArticleById(int stuId) {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        ArrayList<Article> articles = new ArrayList<>();
        try {
            conn = DBUtil.getConn();
            String sql = "select * from article where studentid = " + stuId;
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            while (rs.next()) {
                Integer id = rs.getInt(1);
                Integer studentId = rs.getInt(2);
                String title = rs.getString(3);
                String content = rs.getString(4);
                java.util.Date date = rs.getDate(5);
                articles.add(new Article(id, studentId, title, content, date));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return articles;
    }

    @Override
    public void insertArticle(Article article) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConn();
            String sql = "insert into article (studentId, title, content, pubTime) values (?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, article.getStudentId());
            ps.setString(2, article.getTitle());
            ps.setString(3, article.getContent());
            java.util.Date date1 =  article.getPubTime();
            System.out.println(date1.toString());
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            df.format(date1);
            System.out.println(df.format(date1));
            ps.setDate(4, Date.valueOf(df.format(date1)));
            int count = ps.executeUpdate();
            if (count > 0) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void updateArticle(Article article) {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConn();
            String sql = "UPDATE studentarticle.article  SET title = ?, content = ?, pubTime = ?  WHERE id = ?";
            ps = conn.prepareStatement(sql);

            ps.setString(1, article.getTitle());
            ps.setString(2, article.getContent());
            java.util.Date date1 =  article.getPubTime();
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            ps.setString(3, df.format(article.getPubTime()));
            ps.setInt(4, article.getId());

            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
//
}
